﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class Moral_Teacher_RuleSettings_SettingEval : System.Web.UI.Page
{
    protected int currHistoryId = 0;
    protected List<MoralityType> listType = new List<MoralityType>();

    protected static int HistoryID 
    {
        get
        {
            int hi = 0;
            if (int.TryParse(HttpContext.Current.Request.Params["historyId"], out hi))
            { 
                return hi;
            }
            return 0;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string s = Request.Params["historyId"];
            int.TryParse(s, out currHistoryId);
            //currHistoryId = GetHistory();
        }
    }

    ///// <summary>
    ///// 获取当前学年学期的historyId
    ///// </summary>
    ///// <returns></returns>
    //private int GetHistory()
    //{
    //    int hisotryId = 0;
    //    int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
    //    int term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;
    //    string schoolMark = Common.GetConstSchoolMark();
    //    Hashtable table = new Hashtable();
    //    table.Add("SchoolMark",schoolMark);
    //    table.Add("SchoolYear",schoolYear);
    //    table.Add("Term",term);
    //    table.Add("RID", currHistoryId);
        
    //    IMoralityHistoryLogic logic = Container<IMoralityHistoryLogic>.Instance;
    //    List<MoralityHistory> list = (List<MoralityHistory>)logic.FindAll(table);

    //    if (list == null || list.Count == 0)
    //    {
    //        int n = logic.InsertNew(schoolMark, schoolYear, term, Common.GetUserInfo().UID);
    //        if (n > 0)
    //            hisotryId = n;
    //    }
    //    else
    //    {
    //        hisotryId = list[0].RID;
    //    }
    //    return hisotryId;

    //}

    protected void btnItemBind_Click(object sender, EventArgs e)
    {
        int typeId = 0;
        string s = "";

        s = txtTypeId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out typeId);

        IMoralityItemLogic logic = Container<IMoralityItemLogic>.Instance;
        Hashtable table = new Hashtable();
        table.Add("SchoolMark",Common.GetConstSchoolMark());
        table.Add("TypeID", typeId);
        List<MoralityItem> list = (List<MoralityItem>) logic.FindAll(table);

        Repeater1.DataSource = list;
        Repeater1.DataBind();
    }

    /// <summary>
    /// 获取causeEvalItem列表
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static  List<MoralityType> GetTypeList(int historyid)
    {
        //Hashtable table = new Hashtable();
        //table.Add("SchoolMark", Common.GetConstSchoolMark());
        //table.Add("HistoryID", historyid);

        List<MoralityType> list = new List<MoralityType>();
        IMoralityTypeLogic logic = Container<IMoralityTypeLogic>.Instance;
        list = (List<MoralityType>)logic.FindAllType(Common.GetConstSchoolMark(), SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm, historyid);
        return list;
    }


    /// <summary>
    /// 增删德育项目
    /// </summary>
    /// <param name="listAdd"></param>
    /// <param name="listDel"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int SaveMoralityItem(MoralityItem[] list,MoralityItem[] delIds)
    {
        List<MoralityItem> listDel = new List<MoralityItem>();
        List<MoralityItem> listAdd = new List<MoralityItem>();
        foreach (MoralityItem item in list)
        {
            listAdd.Add(item);
            if (item.RID > 0)
                listDel.Add(item);
        }

        if (delIds.Length > 0)
        {
            foreach (MoralityItem item2 in delIds)
            {
                listDel.Add(item2);
            }
        }

        IMoralityItemLogic logic = Container<IMoralityItemLogic>.Instance;
        int n = logic.SaveMoralityItem(Common.GetConstSchoolMark(),listAdd, listDel);

        return n;
    }

    /// <summary>
    /// 确定保存
    /// </summary>
    /// <param name="historyId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int SaveStatus(int historyId)
    {
        IMoralityHistoryLogic logic = Container<IMoralityHistoryLogic>.Instance;
        int n = logic.UpdateStatus(Common.GetConstSchoolMark(), historyId, 1);
        return n;
    }


    #region 类型的增删改

    /// <summary>
    /// 验证类型名是否已被使用
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <param name="typeName"></param>
    /// <returns>0-未被使用，大于0被使用</returns>
    [System.Web.Services.WebMethod]
    public static int ValidateTypeName(string typeName)
    {
        int n = 0;
        int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        int term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;
        IMoralityTypeLogic logic = Container<IMoralityTypeLogic>.Instance;
        n = logic.ValidateTypeName(Common.GetConstSchoolMark(),schoolYear,term, typeName);
        return n;
    }


    [System.Web.Services.WebMethod]
    public static int AddType(int historyId, string typeName)
    {
        IMoralityTypeLogic logic = Container<IMoralityTypeLogic>.Instance;
        int n = 0;
        if (historyId > 0)
        {
            //已存在于history表的
            n = logic.InsertType(Common.GetConstSchoolMark(), historyId, typeName);
        }
        else
        {
            //需重新创建history之后再创建type的
            int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
            int term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;

            n = logic.InsertTypeHistory(Common.GetConstSchoolMark(),typeName, schoolYear, term, Common.GetUserInfo().UID);
        }
        return n;
    }


    [System.Web.Services.WebMethod]
    public static int DelTypeById(int typeId)
    {
        IMoralityTypeLogic logic = Container<IMoralityTypeLogic>.Instance;
        return logic.DeleteById(Common.GetConstSchoolMark(), typeId);
    }


    [System.Web.Services.WebMethod]
    public static int UpdateType(int typeId, string typeName)
    {
        IMoralityTypeLogic logic = Container<IMoralityTypeLogic>.Instance;
        return logic.UpdateTypeName(Common.GetConstSchoolMark(),typeId, typeName);
    }
    #endregion


    #region 绑定数据后 需要处理的前台效果 比如选中状态

    //做个点击效果
    protected void SetTypeArr()
    {
        Response.Write("currTypeId = 0;");
        Response.Write("currTypeName = \"\";");
        Response.Write("var ul = document.getElementById('ul');");
        Response.Write("var listA = document.getElementsByTagName('a');");
        Response.Write("if(listA.length > 0)");
        Response.Write("{");
        Response.Write("    for(var i =0;i < listA.length;i++)");
        Response.Write("    {");
        Response.Write("        if(i==0)");
        Response.Write("        {");
        Response.Write("            listA[i].click();");
        Response.Write("            currTypeId = listA[i].id;");
        Response.Write("            currTypeName = listA[i].innerText;");
        Response.Write("        }");
        Response.Write("    }");
        Response.Write("}");

    }

    protected string GetTypeOpt(int type)
    {
        if (type < 1 || type > 2)
            return "";

        string s = "";
        if (type == 1)
        {
            s = "<option value='1' selected='selected'>计分</option>" +
            "  <option value='2'>计次</option>";
        }
        if (type == 2)
        {
            s = "<option value='1'>计分</option>" +
           "  <option value='2' selected='selected'>计次</option>";
        }
        return s;
    }

    protected string GetOperatorOpt(int nOperator)
    {
        string s = "";
        if (nOperator == 1)
        {
            s = "<option value='1' selected='selected'>加分</option>" +
              "<option value='-1'>减分</option>";
        }
        else
        {
            s = "<option value='1'>加分</option>" +
             "<option value='-1' selected='selected'>减分</option>";
        }
        return s;
    }

    protected string GetDisplay(int itemType)
    {
        string s = "";
        if (itemType == 1)
        {
            s = "none";
        }
        else
        {
            s = "block";
        }
        return s;
    }
    #endregion

}
